package com.ruoyi.shopInfo.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.basic.domain.AtbShap;
import com.ruoyi.basic.mapper.AtbShapMapper;
import com.ruoyi.common.filter.ID;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.objectPic.domain.AtbObjectPic;
import com.ruoyi.objectPic.mapper.AtbObjectPicMapper;
import com.ruoyi.shopInfo.domain.AtbShopInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shopInfo.mapper.AtbShopInfoMapper;
import com.ruoyi.shopInfo.domain.AtbShopInfo;
import com.ruoyi.shopInfo.service.IAtbShopInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商铺详情信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-16
 */
@Service
public class AtbShopInfoServiceImpl implements IAtbShopInfoService 
{
    @Autowired
    private AtbShopInfoMapper atbShopInfoMapper;

    @Autowired
    private AtbShapMapper atbShapMapper;

    @Autowired
    private AtbObjectPicMapper atbObjectPicMapper;

    /**
     * 查询商铺详情信息
     * 
     * @param id 商铺详情信息主键
     * @return 商铺详情信息
     */
    @Override
    public AtbShopInfo selectAtbShopInfoById(Long id)
    {
        return atbShopInfoMapper.selectAtbShopInfoById(id);
    }

    /**
     * 查询商铺详情信息列表
     * 
     * @param atbShopInfo 商铺详情信息
     * @return 商铺详情信息
     */
    @Override
    public List<AtbShopInfo> selectAtbShopInfoList(AtbShopInfo atbShopInfo)
    {
        return atbShopInfoMapper.selectAtbShopInfoList(atbShopInfo);
    }

    /**
     * 新增商铺详情信息
     * 
     * @param atbShopInfoDto 商铺详情信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAtbShopInfo(AtbShopInfoDto atbShopInfoDto)
    {
        ID id = new ID();
        long id1 = id.getId();

        AtbShap atbShap = new AtbShap();
        atbShap.setId(id1);
        atbShap.setCreateTime(DateUtils.getNowDate());
        atbShap.setUpdateTime(DateUtils.getNowDate());
        atbShap.setShopMessage(atbShopInfoDto.getShopMessage());
        atbShap.setAddress(atbShopInfoDto.getAddress());
        atbShap.setAddressLatitude(atbShopInfoDto.getAddressLatitude());
        atbShap.setAddressLongitude(atbShopInfoDto.getAddressLongitude());
        atbShap.setCity1(atbShopInfoDto.getCity1());
        atbShap.setCity2(atbShopInfoDto.getCity2());
        atbShap.setCity3(atbShopInfoDto.getCity3());
        atbShap.setShopDistance(atbShopInfoDto.getShopDistance());
        atbShap.setMovies(atbShopInfoDto.getMovies());
        atbShapMapper.insertAtbShap(atbShap);



        AtbShopInfo atbShopInfo = BeanUtil.toBean(atbShopInfoDto, AtbShopInfo.class);
        atbShopInfo.setId(id1);
        atbShopInfo.setShopMessageInfo(atbShopInfoDto.getShopMessageInfo());
        atbShopInfo.setShopSell(atbShopInfoDto.getShopSell());
        atbShopInfo.setShopPay(atbShopInfoDto.getShopPay());
        atbShopInfo.setShopStatus(atbShopInfoDto.getShopStatus());
        atbShopInfo.setShopTypeId(atbShopInfoDto.getShopTypeId());
        atbShopInfo.setFloor(atbShopInfoDto.getFloor());
        atbShopInfo.setTenancyTerm(atbShopInfoDto.getTenancyTerm());
        atbShopInfo.setCreateTime(DateUtils.getNowDate());
        atbShopInfo.setUpdateTime(DateUtils.getNowDate());
        atbShopInfo.setPassengerFlow(atbShopInfoDto.getPassengerFlow());


        String[] split = atbShap.getMovies().split(",");
        for (String s : split) {

            //图片添加
            AtbObjectPic atbObjectPic = new AtbObjectPic();
            atbObjectPic.setUrl(s);
            atbObjectPic.setObjectId(atbShap.getId());
            atbObjectPicMapper.insertAtbObjectPic(atbObjectPic);
        }



        return atbShopInfoMapper.insertAtbShopInfo(atbShopInfo);
    }





    /**
     * 修改商铺详情信息
     * 
     * @param atbShopInfo 商铺详情信息
     * @return 结果
     */
    @Override
    public int updateAtbShopInfo(AtbShopInfo atbShopInfo)
    {
        return atbShopInfoMapper.updateAtbShopInfo(atbShopInfo);
    }

    /**
     * 批量删除商铺详情信息
     * 
     * @param ids 需要删除的商铺详情信息主键
     * @return 结果
     */
    @Override
    public int deleteAtbShopInfoByIds(Long[] ids)
    {


        for (Long id : ids) {
            List<AtbObjectPic> atbObjectPicList  = atbObjectPicMapper.selectList(id);

            for (AtbObjectPic atbObjectPic : atbObjectPicList) {
                atbObjectPicMapper.deleteAtbObjectPicById(atbObjectPic.getId());
            }

        }
        atbShapMapper.deleteAtbShapByIds(ids);


        return atbShopInfoMapper.deleteAtbShopInfoByIds(ids);
    }

    /**
     * 删除商铺详情信息信息
     * 
     * @param id 商铺详情信息主键
     * @return 结果
     */
    @Override
    public int deleteAtbShopInfoById(Long id)
    {


        //根据id查看图片图片集合

       List<AtbObjectPic> atbObjectPicList  = atbObjectPicMapper.selectList(id);

        for (AtbObjectPic atbObjectPic : atbObjectPicList) {
            atbObjectPicMapper.deleteAtbObjectPicById(atbObjectPic.getId());
        }
        atbShapMapper.deleteAtbShapById(id);

        return atbShopInfoMapper.deleteAtbShopInfoById(id);
    }
}
